from typing import Optional
import allure
from api.input_api import input_code_one, input_code_two
from tools.logger import log_info, log_debug, log_error
import time


def bind_and_assert(main_win, code_one: str, code_two: Optional[str], type1: str, type2: str, assertion_func, static: bool = True):
    log_info("开始执行条码绑定流程...")

    try:
        with allure.step(f"输入{type1}: '{code_one}'"):
            input_code_one(main_win, code_one, type1, raise_on_error=static)

        if code_two is not None:
            time.sleep(0.2)
            with allure.step(f"输入{type2}: '{code_two}'"):
                input_code_two(main_win, code_two, type2, raise_on_error=static)
        else:
            with allure.step(f"跳过{type2}输入"):
                log_debug("跳过第二个条码输入（code_two=None）")

    except Exception as e:
        log_error(f"🛑 条码输入阶段失败: {e}")
        raise

    with allure.step("执行绑定断言"):
        log_info(f"正在执行断言：{assertion_func.__name__}")
        assertion_func(main_win)
